﻿using NPOI.XWPF.UserModel;
using ProjectCommonLibs.models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ProjectCommonLibs.repository
{
    public class WordRepository
    {
        private readonly string fileName;
        public WordRepository(string fileName)
        {
            this.fileName = fileName;
        }

        public List<DaFengDataModel> readDaFengTableData()
        {
            List<DaFengDataModel> models = new List<DaFengDataModel>();
            XWPFDocument doc = new XWPFDocument(new FileStream(fileName, FileMode.Open, FileAccess.Read));
            var tables = doc.Tables;
            tables = tables.Where(t => tables.IndexOf(t) != 10 && tables.IndexOf(t) != 12).ToList();
            for (int i = 9; i < tables.Count; i++)
            {
                var deviceData = tables[i];
                var rows = deviceData.Rows;
                for (int k = 3; k < rows.Count; k++)
                {
                    var cells = rows[k].GetTableCells();
                    models.Add(new DaFengDataModel() { 
                        deviceName = String.Format("D{0:D2}",i - 8),
                        timeSpan = "2021-" + cells[0].GetText(),
                        count = Convert.ToInt32(cells[1].GetText()),
                        s3 = Convert.ToInt32(cells[2].GetText()),
                        s4 = Convert.ToInt32(cells[3].GetText()),
                        s5 = Convert.ToInt32(cells[4].GetText()),
                        s6 = Convert.ToInt32(cells[5].GetText()),
                        s7 = Convert.ToInt32(cells[6].GetText()),
                        s8 = Convert.ToInt32(cells[7].GetText()),
                        s9 = Convert.ToInt32(cells[8].GetText()),
                        s10 = Convert.ToInt32(cells[9].GetText()),
                        s11 = Convert.ToInt32(cells[10].GetText()),
                        s12 = Convert.ToInt32(cells[11].GetText()),
                        s13 = Convert.ToInt32(cells[12].GetText()),
                        s14 = Convert.ToInt32(cells[13].GetText()),
                        s15= Convert.ToInt32(cells[14].GetText()),
                        s16= Convert.ToInt32(cells[15].GetText()),
                        s17= Convert.ToInt32(cells[16].GetText()),
                        s18= Convert.ToInt32(cells[17].GetText()),
                        s19= Convert.ToInt32(cells[18].GetText()),
                        s20= Convert.ToInt32(cells[19].GetText()),
                        produceFactor = Convert.ToDouble(cells[20].GetText())
                    });
                }
            }
            return models;
        }
    }
}
